package eSchool.GUI.Controls;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

public class DataGrid extends JTable {

    private TableModel tableModel;
    private TableModel dataSource;
    private DataGridColumnIdentifiers columnIdentifiers;

    public DataGrid() {
        super();
        tableModel = new DefaultTableModel() {
            //reescrevendo o metodo para retornar que todas as linhas serao read-only 
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        };
        this.setModel(tableModel);
    }

    public void setColumnIdentifiers(DataGridColumnIdentifiers columnIdentifiers) {
        this.columnIdentifiers = columnIdentifiers;
        setColumnLayout();
    }

    public void setDataSource(TableModel dataSource) {
        this.dataSource = dataSource;
    }

    public void dataBind() throws Exception {

        if (this.dataSource == null) {
            throw new Exception("DataSource indefinido!");
        }
        tableModel = dataSource;

        this.setModel(tableModel);

        setColumnLayout();
    }

    private void setColumnLayout() {
        if (columnIdentifiers != null) {
            int max = tableModel.getColumnCount();
            for(int i = this.getColumnModel().getColumnCount(); i < max; i++ )
                this.getColumnModel().addColumn(new TableColumn());
            
            int x = 0;
            for (int y = 0; y < max; y++) {
                boolean enc = false;
                for (int i = 0; i < columnIdentifiers.size(); i++) {
                    if (tableModel.getColumnName(x).equals(columnIdentifiers.get(i).getBindColuna())) {
                        enc = true;
                        break;
                    }
                }
                if (!enc) {
                    if (this.getColumnModel().getColumn(y) != null) {
                        this.getColumnModel().removeColumn(this.getColumnModel().getColumn(y));
                        max--;
                        y--;
                    }
                }
                x++;
            }
            for (int y = 0; y < max; y++) {
                for (int i = 0; i < columnIdentifiers.size(); i++) {
                    if (tableModel.getColumnName(y).equals(columnIdentifiers.get(i).getBindColuna())) {
                        this.getColumnModel().moveColumn(y, i);
                    }
                }

            }

            if (this.getColumnModel().getColumnCount() == columnIdentifiers.size()) {
                for (int i = 0; i < columnIdentifiers.size(); i++) {
                    this.getColumnModel().getColumn(i).setPreferredWidth(columnIdentifiers.get(i).getTamanho());
                    this.getColumnModel().getColumn(i).setHeaderValue(columnIdentifiers.get(i).getNomeColuna());
                }
            }
        }

    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public void consDataGrid(String[] cabecalho, int[] largura, IDadosEntidade dados) {
        DefaultTableModel defaultTableModel = new DefaultTableModel() {
            //reescrevendo o metodo para retornar que todas as linhas serao read-only 
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        };



        defaultTableModel.setColumnIdentifiers(cabecalho);

        String[][] sDados = dados.getDadosEntidade();
        for (int i = 0; i < sDados.length; i++) {
            defaultTableModel.addRow(sDados[i]);
        }

        this.setModel(defaultTableModel);
        this.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        for (int i = 0; i < largura.length; i++) {
            this.getColumnModel().getColumn(i).setPreferredWidth(largura[i]);
        }

//          dataGrid1.setModel(new javax.swing.table.DefaultTableModel(
//            new Object [][] {
//                {null, null, null, null},
//                {null, null, null, null},
//                {null, null, null, null},
//                {null, null, null, null}
//            },
//            new String [] {
//                "Title 1", "Title 2", "Title 3", "Title 4"
//            }
////        ));
//         jScrollPane1.setViewportView(dataGrid1);
//
//        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
//        this.setLayout(layout);
//        layout.setHorizontalGroup(
//            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGroup(layout.createSequentialGroup()
//                .addContainerGap()
//                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
//                .addContainerGap())
//        );
//        layout.setVerticalGroup(
//            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
//                .addContainerGap(139, Short.MAX_VALUE)
//                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
//                .addContainerGap())
//        );

    }
}
